package models

import (
	"github.com/astaxie/beego"
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
)

type Blog struct {
	Id            int64     `json:"id" orm:"pk;auto"`
	BlogName      string    `json:"blog_name"`
	BlogContent   string    `json:"blog_content" orm:"type(text)"`
	BlogState     int8      `json:"blog_state"`
	BlogTop       int8      `json:"blog_top"`
	BlogPicture   string    `json:"blog_picture"`
	BlogShowWords string    `json:"blog_show_words" orm:"type(text)"`
	View          int       `json:"view"`
	UpdateAt      time.Time `json:"update_at"`
	Types         []*Type   `json:"types" orm:"rel(m2m)"`
	Labels        []*Label  `json:"labels" orm:"rel(m2m)"`
}

func (b *Blog) TableName() string {
	return TableName("blog")
}

// GetViews 获取该博客的浏览量
func (b *Blog) GetViews() (n int, err error) {
	get := Conn()
	blogViews := "blogViews:\"" + strconv.Itoa(int(b.Id)) + "\""
	n, err = redis.Int(get.Do("get", blogViews))
	if err != nil {
		n = 0
	}
	return n, err
}

// GetAllViews 获取总的浏览量
func (b *Blog) GetAllViews() (n int, err error) {
	get := Conn()
	allBlogViews := "allBlogViews"
	n, err = redis.Int(get.Do("Get", allBlogViews))
	if err != nil {
		n = 0
	}
	return
}

// GetAllViewsAWeek 一周浏览量
func (b *Blog) GetAllViewsAWeek() (n int, err error) {
	get := Conn()
	viewsInADayOne := "blogViews:" + time.Unix(time.Now().Add(-7*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayTwo := "blogViews:" + time.Unix(time.Now().Add(-6*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayThree := "blogViews:" + time.Unix(time.Now().Add(-5*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFour := "blogViews:" + time.Unix(time.Now().Add(-4*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFive := "blogViews:" + time.Unix(time.Now().Add(-3*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySix := "blogViews:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySenven := "blogViews:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	one, err := redis.Int(get.Do("Get", viewsInADayOne))
	if err != nil {
		one = 0
	}
	two, err := redis.Int(get.Do("Get", viewsInADayTwo))
	if err != nil {
		two = 0
	}
	three, err := redis.Int(get.Do("Get", viewsInADayThree))
	if err != nil {
		three = 0
	}
	four, err := redis.Int(get.Do("Get", viewsInADayFour))
	if err != nil {
		four = 0
	}
	five, err := redis.Int(get.Do("Get", viewsInADayFive))
	if err != nil {
		five = 0
	}
	six, err := redis.Int(get.Do("Get", viewsInADaySix))
	if err != nil {
		six = 0
	}
	senven, err := redis.Int(get.Do("Get", viewsInADaySenven))
	if err != nil {
		senven = 0
	}
	n = one + two + three + four + five + six + senven
	return
}

// GetEveryDayViewsInWeek 获取一周中每天的浏览量
func (b *Blog) GetEveryDayViewsInWeek() (n []int, err error) {
	get := Conn()
	viewsInADayOne := "blogViews:" + time.Unix(time.Now().Add(-7*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayTwo := "blogViews:" + time.Unix(time.Now().Add(-6*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayThree := "blogViews:" + time.Unix(time.Now().Add(-5*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFour := "blogViews:" + time.Unix(time.Now().Add(-4*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFive := "blogViews:" + time.Unix(time.Now().Add(-3*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySix := "blogViews:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySenven := "blogViews:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	one, err := redis.Int(get.Do("Get", viewsInADayOne))
	if err != nil {
		one = 0
	}
	n = append(n, one)
	two, err := redis.Int(get.Do("Get", viewsInADayTwo))
	if err != nil {
		two = 0
	}
	n = append(n, two)
	three, err := redis.Int(get.Do("Get", viewsInADayThree))
	if err != nil {
		three = 0
	}
	n = append(n, three)
	four, err := redis.Int(get.Do("Get", viewsInADayFour))
	if err != nil {
		four = 0
	}
	n = append(n, four)
	five, err := redis.Int(get.Do("Get", viewsInADayFive))
	if err != nil {
		five = 0
	}
	n = append(n, five)
	six, err := redis.Int(get.Do("Get", viewsInADaySix))
	if err != nil {
		six = 0
	}
	n = append(n, six)
	senven, err := redis.Int(get.Do("Get", viewsInADaySenven))
	if err != nil {
		senven = 0
	}
	n = append(n, senven)
	return
}

// AddViews 增加该博客的浏览量
func (b *Blog) AddViews(id string) (err error) {
	get := Conn()
	blogViews := "blogViews:" + id
	beego.Error(blogViews)
	a, err := get.Do("incr", blogViews)
	beego.Error(a)
	return
}

// AddAllViews 增加所有博客的浏览量
func (b *Blog) AddAllViews() (err error) {
	get := Conn()
	allBlogViews := "allBlogViews"
	_, err = get.Do("incr", allBlogViews)
	return
}

// AddDayViews 增加博客日浏览量
func (b *Blog) AddDayViews() (err error) {
	get := Conn()
	viewsInADay := "blogViews:" + time.Unix(time.Now().Unix(), 0).String()[:10]
	_, err = get.Do("incr", viewsInADay)
	return
}
